package com.zfzs.post.modules.transaction.service.impl;

import com.zfzs.post.modules.agent.domain.AgentMoney;
import com.zfzs.post.modules.agent.service.AgentMoneyService;
import com.zfzs.post.modules.agent.service.PolicyDividedService;
import com.zfzs.post.modules.agentinfo.service.AgentWebService;
import com.zfzs.post.modules.backstage.service.BackStageUserService;
import com.zfzs.post.modules.common.service.AgentContactService;
import com.zfzs.post.modules.finance.service.BenefitWebService;
import com.zfzs.post.modules.merchant.domain.Merchant;
import com.zfzs.post.modules.merchant.service.MerchantService;
import com.zfzs.post.modules.transaction.domain.TransactionInfo;
import com.zfzs.post.modules.transaction.service.TransactionInfoService;
import com.zfzs.post.modules.transaction.service.TransactionWebService;
import com.zfzs.post.utils.DateStrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author wq
 * @date 2019/11/27 17:04
 * @description
 */
@Service
public class TransactionWebServiceImpl implements TransactionWebService {

    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private AgentContactService agentContactService;

    @Autowired
    private BackStageUserService backStageUserService;


    @Autowired
    private PolicyDividedService policyDividedService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private AgentMoneyService agentMoneyService;

    @Autowired
    private BenefitWebService benefitWebService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void benefitAllTransaction() {

        //查询未计算分润的交易
        List<Long> channelList=backStageUserService.findAllChannelId();

        String str = DateStrUtils.getYesterdayStr("yyyy-MM-dd");

        for (Long channelId : channelList) {
            List<TransactionInfo> infoList = transactionInfoService.queryNotBenefitTransaction(str, channelId);

            infoList=filterTransaction(infoList,channelId);

            if(!infoList.isEmpty()){
                for (TransactionInfo info : infoList) {
                    benefitTransaction(info);
                }

            }
        }


    }


    @Override
    public List<TransactionInfo> filterTransaction(List<TransactionInfo> infoList, Long channelId) {
        return infoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void benefitTransaction(TransactionInfo info) {

        Merchant merchant = merchantService.findBySn(info.getSn());
        if(merchant==null){
            return;
        }

        //计算费率
        BigDecimal amount = info.getAmount();

        BigDecimal rateAmount = info.getRateAmount();

        rateAmount=rateAmount.subtract(new BigDecimal("3.00"));

        BigDecimal rateType = rateAmount.multiply(new BigDecimal(100)).divide(amount,BigDecimal.ROUND_HALF_UP);

        String names=rateType+"类型";

        //先计算个人的分润
        BigDecimal rate = policyDividedService.getPolicyAmount(info.getAgentId(), info.getTerminalTypeId(), info.getChannelId(),names);

        if(rate.compareTo(new BigDecimal("0"))==1){
            BigDecimal benefit = amount.multiply(rate).divide(new BigDecimal("1000"), BigDecimal.ROUND_HALF_UP);


            agentMoneyService.addAgentMoney(info.getAgentId(),benefit);
            //添加个人的分润记录

            benefitWebService.addSelfBenefitDay(info,benefit);
        }



        List<Long> idList = agentContactService.findAllParentIdList(info.getAgentId(), info.getChannelId());

        //计算上级的分润
        for (int i = 0; i < idList.size(); i++) {

                if(i<idList.size()-1){
                    Long childId=idList.get(i);

                    Long benefitAgentId=idList.get(i+1);
                    //先计算个人的分润
                    BigDecimal childRate = policyDividedService.getPolicyAmount(childId, info.getTerminalTypeId(), info.getChannelId(),names);

                    BigDecimal benefitAgentRate = policyDividedService.getPolicyAmount(benefitAgentId, info.getTerminalTypeId(), info.getChannelId(),names);

                    if(benefitAgentRate.compareTo(childRate)==1){

                        BigDecimal realRate = benefitAgentRate.subtract(childRate);

                        BigDecimal teamBenefit = amount.multiply(realRate).divide(new BigDecimal("1000"), BigDecimal.ROUND_HALF_UP);

                        agentMoneyService.addAgentMoney(benefitAgentId,teamBenefit);

                        benefitWebService.addTeamBenefitDay(info,teamBenefit,benefitAgentId);

                    }

                }
        }
        info.setIsBenefited(1);
        transactionInfoService.update(info);
    }

}
